home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10440 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.4 KB

  1. Path: inforamp.net!ts9-03
  2. From: rmorin@inforamp.net (Randy Charles Morin)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Printing class names
  5. Date: Thu, 07 Mar 96 05:57:12 GMT
  6. Organization: MiddleWorld SoftWare
  7. Message-ID: <4hltri$6jg@sam.inforamp.net>
  8. References: <4hj95e$cou@hermes.is.co.za>
  9. NNTP-Posting-Host: ts9-03.tor.inforamp.net
  10. X-Newsreader: News Xpress Version 1.0 Beta #4
  11.  
  12. In article <4hj95e$cou@hermes.is.co.za>, "W. Dicks" <wd@isis.co.za> wrote:
  13. >I have a class called VeryLongNum with a derived class 
  14. >VeryLongEvenNum. I then create instances of both types and add 
  15. >them to a linked list. Later I print the values of these 
  16. >numbers using the linked list's print functionality. The 
  17. >VeryLongNum has an operator<< defined to print to stdout. The 
  18. >functionality that I would like to add to the number classes 
  19. >is to be able to print the type of the number based on the 
  20. >class when the value of the number is printed. How would I do 
  21. >this?
  22.  
  23. I would define a virual write class name member function for the parent and 
  24. the child class.  The operator<< can call this class.  Example:
  25.  
  26. virtual void VeryLongNum::WriteClassName(opstream& op)
  27. {
  28.     op << "VeryLongNum";
  29. };
  30.  
  31. virtual void VeryLongEvenNum::WriteClassName(opstream& op)
  32. {
  33.     op << "VeryLongEvenNum";
  34. };
  35.  
  36. friend opstream& operator << (opstream&  ps, VeryLongNum& r)
  37. {
  38.     ...
  39.     r.WriteClassName(ps);
  40.     ...
  41. };
  42.  
  43. I hope that's what you are looking for.
  44.  
  45. Agrivar
  46.